<template>
  <div class="flow-chart-container">
    <div class="flow-chart-header" v-if="title">
      <h3 class="text-lg font-semibold">{{ title }}</h3>
    </div>
    <div class="flow-chart-content">
      <div class="flow-steps">
        <div
          v-for="(step, index) in steps"
          :key="step.id || index"
          class="flow-step"
          :class="{
            'step-completed': step.status === 'completed',
            'step-current': step.status === 'current',
            'step-pending': step.status === 'pending',
            'step-error': step.status === 'error',
            'step-disabled': step.status === 'disabled'
          }"
        >
          <!-- 步骤节点 -->
          <div class="step-node" @click="handleStepClick(step, index)">
            <div class="step-icon">
              <Icon v-if="step.icon" :icon="step.icon" />
              <Icon v-else-if="step.status === 'completed'" icon="ep:check" />
              <Icon v-else-if="step.status === 'error'" icon="ep:close" />
              <Icon v-else-if="step.status === 'current'" icon="ep:loading" class="animate-spin" />
              <span v-else class="step-number">{{ index + 1 }}</span>
            </div>
          </div>

          <!-- 步骤标题和描述 -->
          <div class="step-content">
            <div class="step-title">{{ step.title }}</div>
            <div class="step-description" v-if="step.description">{{ step.description }}</div>
            <div class="step-time" v-if="step.time">{{ formatTime(step.time) }}</div>
            <div class="step-operator" v-if="step.operator">操作人：{{ step.operator }}</div>
          </div>

          <!-- 连接线 -->
          <div v-if="index < steps.length - 1" class="step-connector">
            <div class="connector-line" :class="{
              'line-completed': isStepCompleted(index + 1),
              'line-current': steps[index + 1]?.status === 'current'
            }"></div>
            <Icon 
              icon="ep:arrow-right" 
              class="connector-arrow"
              :class="{
                'arrow-completed': isStepCompleted(index + 1),
                'arrow-current': steps[index + 1]?.status === 'current'
              }"
            />
          </div>
        </div>
      </div>

      <!-- 详细信息面板 -->
      <div v-if="selectedStep && showDetail" class="step-detail-panel">
        <div class="detail-header">
          <h4>{{ selectedStep.title }}</h4>
          <el-button link @click="closeDetail">
            <Icon icon="ep:close" />
          </el-button>
        </div>
        <div class="detail-content">
          <div class="detail-item" v-if="selectedStep.description">
            <label>描述：</label>
            <span>{{ selectedStep.description }}</span>
          </div>
          <div class="detail-item" v-if="selectedStep.time">
            <label>时间：</label>
            <span>{{ formatTime(selectedStep.time) }}</span>
          </div>
          <div class="detail-item" v-if="selectedStep.operator">
            <label>操作人：</label>
            <span>{{ selectedStep.operator }}</span>
          </div>
          <div class="detail-item" v-if="selectedStep.comments">
            <label>备注：</label>
            <span>{{ selectedStep.comments }}</span>
          </div>
          <div class="detail-item" v-if="selectedStep.attachments && selectedStep.attachments.length > 0">
            <label>附件：</label>
            <div class="attachments">
              <el-link
                v-for="file in selectedStep.attachments"
                :key="file.id"
                :href="file.url"
                target="_blank"
                class="attachment-link"
              >
                <Icon icon="ep:document" class="mr-1" />
                {{ file.name }}
              </el-link>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { formatDateTime } from '@/utils/formatTime'

interface FlowStep {
  id?: string | number
  title: string
  description?: string
  status: 'pending' | 'current' | 'completed' | 'error' | 'disabled'
  icon?: string
  time?: string | Date
  operator?: string
  comments?: string
  attachments?: Array<{
    id: string | number
    name: string
    url: string
  }>
}

interface Props {
  /** 流程标题 */
  title?: string
  /** 流程步骤 */
  steps: FlowStep[]
  /** 是否显示详细信息 */
  showDetail?: boolean
  /** 是否可点击 */
  clickable?: boolean
  /** 布局方向 */
  direction?: 'horizontal' | 'vertical'
}

const props = withDefaults(defineProps<Props>(), {
  showDetail: true,
  clickable: true,
  direction: 'horizontal'
})

const emit = defineEmits<{
  'step-click': [step: FlowStep, index: number]
  'step-change': [step: FlowStep, index: number]
}>()

const selectedStep = ref<FlowStep | null>(null)

/** 处理步骤点击 */
const handleStepClick = (step: FlowStep, index: number) => {
  if (!props.clickable) return
  
  selectedStep.value = step
  emit('step-click', step, index)
}

/** 关闭详情 */
const closeDetail = () => {
  selectedStep.value = null
}

/** 判断步骤是否已完成 */
const isStepCompleted = (index: number) => {
  return props.steps[index]?.status === 'completed'
}

/** 格式化时间 */
const formatTime = (time: string | Date) => {
  if (!time) return ''
  return formatDateTime(time)
}

/** 监听步骤变化 */
watch(
  () => props.steps,
  (newSteps) => {
    // 找到当前步骤
    const currentStep = newSteps.find(step => step.status === 'current')
    if (currentStep) {
      const index = newSteps.indexOf(currentStep)
      emit('step-change', currentStep, index)
    }
  },
  { deep: true }
)
</script>

<style scoped>
.flow-chart-container {
  width: 100%;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.flow-chart-header {
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #e5e7eb;
}

.flow-steps {
  display: flex;
  align-items: flex-start;
  position: relative;
  overflow-x: auto;
  padding: 20px 0;
}

.flow-step {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  min-width: 200px;
  margin-right: 40px;
}

.flow-step:last-child {
  margin-right: 0;
}

.step-node {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 10px;
}

.step-pending .step-node {
  background: #f3f4f6;
  border: 2px solid #d1d5db;
  color: #6b7280;
}

.step-current .step-node {
  background: #dbeafe;
  border: 2px solid #3b82f6;
  color: #3b82f6;
}

.step-completed .step-node {
  background: #dcfce7;
  border: 2px solid #16a34a;
  color: #16a34a;
}

.step-error .step-node {
  background: #fef2f2;
  border: 2px solid #dc2626;
  color: #dc2626;
}

.step-disabled .step-node {
  background: #f9fafb;
  border: 2px solid #e5e7eb;
  color: #9ca3af;
  cursor: not-allowed;
}

.step-icon {
  font-size: 24px;
}

.step-number {
  font-size: 18px;
  font-weight: bold;
}

.step-content {
  text-align: center;
  max-width: 180px;
}

.step-title {
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 4px;
  color: #374151;
}

.step-description {
  font-size: 12px;
  color: #6b7280;
  margin-bottom: 4px;
}

.step-time {
  font-size: 11px;
  color: #9ca3af;
  margin-bottom: 2px;
}

.step-operator {
  font-size: 11px;
  color: #9ca3af;
}

.step-connector {
  position: absolute;
  top: 30px;
  right: -40px;
  width: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.connector-line {
  width: 30px;
  height: 2px;
  background: #e5e7eb;
  transition: all 0.3s ease;
}

.line-completed {
  background: #16a34a;
}

.line-current {
  background: #3b82f6;
}

.connector-arrow {
  position: absolute;
  right: -2px;
  color: #e5e7eb;
  transition: all 0.3s ease;
}

.arrow-completed {
  color: #16a34a;
}

.arrow-current {
  color: #3b82f6;
}

.step-detail-panel {
  margin-top: 30px;
  padding: 20px;
  background: #f9fafb;
  border-radius: 8px;
  border: 1px solid #e5e7eb;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #e5e7eb;
}

.detail-header h4 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #374151;
}

.detail-item {
  display: flex;
  margin-bottom: 10px;
}

.detail-item label {
  min-width: 80px;
  font-weight: 500;
  color: #374151;
}

.detail-item span {
  color: #6b7280;
}

.attachments {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.attachment-link {
  display: flex;
  align-items: center;
}

.animate-spin {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
